package com.fgr.controller;

import com.fgr.beans.Donation;
import com.fgr.service.DonationService;
import com.fgr.service.DonationTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class DonationController {

    @Autowired
    DonationService donationService;

    @PostMapping("GetDonationType")
    @ResponseBody
    public String DetermineLoginOffer(String dnType,Integer dnTid,HttpSession httpSession){
        Object obj = httpSession.getAttribute("userid");
        if (obj==null){
            return "Login";
        }
        httpSession.setAttribute("dnType",dnType);
        httpSession.setAttribute("dnTid",dnTid);

        return "SUCCESS";

    }


    @ResponseBody
    @PostMapping("addDonation")
    public String addDonation(Donation donation, HttpSession httpSession){
        String userid =httpSession.getAttribute("userid").toString();
        String dnTid = httpSession.getAttribute("dnTid").toString();

        donation.setDnUid(Integer.parseInt(userid));
        donation.setDnTid(Integer.parseInt(dnTid));

        donation.setDnPublish(0);
        String s = donationService.insertDonation(donation);
        return s;

    }


    //这里增加的是直接审核通过的
    @ResponseBody
    @PostMapping("addOfferBg")
    public String addOfferBg(Donation donation){
        donation.setDnPublish(1);
        String s = donationService.insertDonation(donation);
        return s;
    }

    @ResponseBody
    @PostMapping("modifyOfferBg")
    public String modifyOfferBg(Donation donation){
        String s = donationService.updateDonation(donation);
        return s;

    }


    @GetMapping("selectDonationAndUserAndTypeAuditing")
    public String selectDonationAndUserAndTypeAuditing(Integer dnId,Model model){
        Donation donation = donationService.selectDonationAndUserAndType(dnId);
        model.addAttribute("donation",donation);
        return "background/auditoffer";
    }


    @GetMapping("selectAllDonationBg")
    public String selectAllDonationBg(Integer publish,Model model){
        List<Donation> donations = donationService.selectAllDonation(publish);
        model.addAttribute("donations",donations);
        if (publish==1){
            return "background/offer-bg";
        }else {
            return "background/auditing-offer";
        }
    }

    @PostMapping("updateDonationPublishByDnId")
    @ResponseBody
    public String updateDonationPublishByDnId(Integer dnId){
        String s = donationService.updateDonationPublishByDnId(dnId);
        return s;

    }

    @ResponseBody
    @PostMapping("deleteDonationByDnIdBg")
    public String deleteDonationByDnIdBg(Integer dnId){
        String s = donationService.deleteDonationByDnId(dnId);
        return s;
    }


}
